<?php

/**
 *    文件上传
 *
 */
class Uploader {
	var $_file = null;
	public $allowed_file_type = null; 	//字符串  gif|jpg|jpeg|png
 	public  $allowed_file_size = null;	//字符串 允许上传的文件大小 字符串(最大允许)或是数组(区间允许)
	var $_root_dir = null;
	var $uploader;
	
	/**
	 * 将上传的文件移动到指定的位置
	 *
	 * @param string $src        	绝对路径
	 * @param string $target        	相对root_dir 的地址,或 root_dir不存在时的绝对路径
	 * @return bool if true mixed
	 */
	function move_uploaded_file($src, $target) {
		$abs_path = $this->_root_dir ? $this->_root_dir . '/' . $target : $target;
		$dirname = dirname ( $target );
		if (! $this->_create_directory ( ROOT_PATH . '/' . $dirname )) {
			$this->_error ( 'dir_doesnt_exists' );
			
			return false;
		}
		
		if (move_uploaded_file ( $src, $abs_path )) {
			@chmod ( $abs_path, 0666 );
			return $target;
		} else {
			return false;
		}
	}
	/**
	 * 获取上传文件的数据
	 */
	public function get_upload_file(){
		
	}
	/**
	 * 取得文件信息
	 * @param $file
	 */
	private function _get_info($file){
		//TODO 检查类型
		//TODO 检查大小
		
	}
	
	/**
	 * 取得文件类型
	 * 
	 * @param string $filename        	
	 * @return string 文件类型
	 */
	protected function _get_mimetype($filename) {
		preg_match ( "|\.([a-z0-9]{2,4})$|i", $filename, $fileSuffix );
		switch (strtolower ( $fileSuffix [1] )) {
			case "js" :
				return "application/x-javascript";
			
			case "json" :
				return "application/json";
			
			case "jpg" :
			case "jpeg" :
			case "jpe" :
				return "image/jpeg";
			
			case "png" :
			case "gif" :
			case "bmp" :
			case "tiff" :
				return "image/" . strtolower ( $fileSuffix [1] );
			
			case "css" :
				return "text/css";
			
			case "xml" :
				return "application/xml";
			
			case "doc" :
			case "docx" :
				return "application/msword";
			
			case "xls" :
			case "xlt" :
			case "xlm" :
			case "xld" :
			case "xla" :
			case "xlc" :
			case "xlw" :
			case "xll" :
				return "application/vnd.ms-excel";
			
			case "ppt" :
			case "pps" :
				return "application/vnd.ms-powerpoint";
			
			case "rtf" :
				return "application/rtf";
			
			case "pdf" :
				return "application/pdf";
			
			case "html" :
			case "htm" :
			case "php" :
				return "text/html";
			
			case "txt" :
				return "text/plain";
			
			case "mpeg" :
			case "mpg" :
			case "mpe" :
				return "video/mpeg";
			
			case "mp3" :
				return "audio/mpeg3";
			
			case "wav" :
				return "audio/wav";
			
			case "aiff" :
			case "aif" :
				return "audio/aiff";
			
			case "avi" :
				return "video/msvideo";
			
			case "wmv" :
				return "video/x-ms-wmv";
			
			case "mov" :
				return "video/quicktime";
			
			case "flv" :
				return 'video/x-flv';
			
			case "rar" :
				return "application/x-rar-compressed";
			
			case "zip" :
				return "application/zip";
			
			case "tar" :
				return "application/x-tar";
			
			case "swf" :
				return "application/x-shockwave-flash";
			
			default :
				if (function_exists ( "mime_content_type" )) {
					$fileSuffix = mime_content_type ( $filename );
					return $fileSuffix;
				}
				return 'application/octet-stream';
		}
	}
	
	/**
	 *
	 * @param string $absolute_path        	
	 * @param
	 *        	$mode
	 * @return boolean
	 */
	private function _create_directory($absolute_path, $mode = 0777) {
		if (is_dir ( $absolute_path )) {
			return true;
		}
		
		$root_path = HOME;
		$relative_path = str_replace ( $root_path, '', $absolute_path );
		$each_path = explode ( '/', $relative_path );
		$cur_path = $root_path; // 当前循环处理的路径
		foreach ( $each_path as $path ) {
			if ($path) {
				$cur_path = $cur_path . '/' . $path;
				if (! is_dir ( $cur_path )) {
					if (@mkdir ( $cur_path, $mode )) {
						fclose ( fopen ( $cur_path . '/index.htm', 'w' ) );
					} else {
						return false;
					}
				}
			}
		}
		
		return true;
	}
	/**
	 * 获取能够上传的文件类型
	 * @return array 充许上传类型数组
	 */
	private function _get_allow_type(){
		return explode('|', $this->allowed_file_type);
	}
	/**
	 * 判断类型是否允许
	 * @param unknown_type $type
	 * @return boolean
	 */
    function _is_allowd_type($type)
    {
        if (!$this->_allowed_file_type)
        {
            return true;
        }
        return in_array(strtolower($type), $this->_get_allow_type());
    }
    /**
     * 判断大小是否允许
     * @param int $size
     * @return boolean
     */
    function _is_allowd_size($size)
    {
        if (!$this->allowed_file_size)
        {
            return true;
        }

        return is_numeric($this->_allowed_file_size) ?
                ($size <= $this->_allowed_file_size) :
                ($size >= $this->_allowed_file_size[0] && $size <= $this->_allowed_file_size[1]);
    }
}
